/**
* Author: Samfu
* Time  : 上午1:46:34
* Desc  : 
*/
package com.fu.moneybag.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.fu.moneybag.dao.ArticleDao;
import com.fu.moneybag.entity.Article;
import com.fu.moneybag.entity.User;
import com.fu.moneybag.service.ArticleService;
import com.fu.moneybag.service.ArticleTypeService;
import com.fu.moneybag.utils.GetResultMap;

public class ArticleServiceImpl implements ArticleService {

	private Logger logger = Logger.getLogger(ArticleServiceImpl.class);

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private ArticleTypeService articleTypeService;

	private GetResultMap getResultMap;

	public GetResultMap getGetResultMap() {
		return getResultMap;
	}

	public void setGetResultMap(GetResultMap getResultMap) {
		this.getResultMap = getResultMap;
	}

	@Override
	public int addArticle(User user, String articleTitle, String articleType, String articleContent) {

		Article article = new Article();// 新建一个article实例
		article.setArticleTitle(articleTitle);
		article.setCreateTime(new Date());
		article.setUpdateTime(new Date());
		article.setArticleAuthor(user.getUserName());
		article.setAuthorId(user.getUserId());
		article.setArticleType(articleType);
		article.setArticleContent(articleContent);

		return articleDao.addArticle(article);
	}

	@Override
	public List<Article> getAllArticles(long userId) {

		return articleDao.getAllArticles(userId);
	}

	@Override
	public Article getArticleByArticleId(long articleId) {
		return articleDao.getArticleByArticleId(articleId);
	}

	@Override
	public int deleteArticleById(long articleId, long authorId) {
		return articleDao.deleteArticleById(articleId, authorId);
	}

	@Override
	public int updateArticleById(String articleTitle, String articleType, String articleContent, long articleId,
			long authorId) {

		return articleDao.updateArticleById(articleTitle, articleType, articleContent, articleId, authorId);
	}

	@Override
	public String createArticle(String articleTitle, String articleType, String articleContent,
			HttpServletRequest request, HttpSession session) {

		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect:/user/login";
		}
		//
		int createArticleResult = this.addArticle(user, articleTitle, articleType, articleContent);

		if (createArticleResult == 1) {
			session.setAttribute("user", user);
			request.setAttribute("createSucceed", "发表文章成功");
		} else {
			request.setAttribute("createFailed", "发表文章失败");
		}
		return this.index(session);
	}

	@Override
	public List<Article> getArticleByType(String articleType) {
		return articleDao.getArticleByType(articleType);
	}

	@Override
	public String deleteArticleById(HttpServletRequest request, HttpSession session, long articleId, long authorId) {
		int deleteResult = this.deleteArticleById(articleId, authorId);
		if (deleteResult != 1) {
			logger.info("删除文章出错");
			request.setAttribute("deleteError", "删除文章出错！");
		}
		return this.index(session);
	}
	
	//跳转到index
	public String index(HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "redirect: /user/login";
		}
		// 跳回主页，刷新类别
		logger.info("跳转到用户首页");
		session.setAttribute("user", user);
		// 获取文章列表
		List<Article> articles = this.getAllArticles(user.getUserId());
		logger.info("文章总数为： " + articles.size());
		session.setAttribute("articles", articles);
		// 获取artilce group by article类型
		List<HashMap<String, Integer>> articleGroups = new ArrayList<HashMap<String, Integer>>();
		articleGroups = articleTypeService.getArticleGroupByType(user.getUserId());
		// 新建一个result map 给前段显示
		HashMap<String, Number> resultMap = getResultMap.getArticleMap(articleGroups);
		session.setAttribute("map", resultMap);
		return "userindex";
	}

<<<<<<< HEAD
	@Override
	public List<Article> getArticleByPage(Integer startPos, Integer pageSize, long userId) {
		return articleDao.getArticleByPage(startPos, pageSize, userId);
	}

	@Override
	public List<Article> getArticleByPageAndType(Integer startPos, Integer pageSize, long userId, String articleType) {

		return articleDao.getArticleByPageAndType(startPos, pageSize, userId, articleType);
	}


=======
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
}
